package com.easy.common.util;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

public class AzDGCrypt {
	public static final String KEY = "MJFY AzDGCrypt";

	@SuppressWarnings("unused")
	public static byte[] encrypt(byte[] txt, byte[] key) {
	    int rand = new Double(Math.random() * 32000.0D).intValue();
	    byte[] encrypt_key = Security.MD5(String.valueOf(rand)).toLowerCase().getBytes();
	    byte ctr = 0;
	    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
	    int i = 0; 
	    for (int j = 0; i < txt.length; j += 2) {
	      ctr = (ctr == encrypt_key.length) ? 0 : ctr;
	      byteOut.write(encrypt_key[ctr]);
	      byte tmp96_94 = ctr;
	      ctr = (byte)(tmp96_94 + 1); 
	      byteOut.write(txt[i] ^ encrypt_key[tmp96_94]);
	      ++i;
	    }
	    return Base64.encode(encodeKey(byteOut.toByteArray(), key)).getBytes();
	}

	public static String encrypt(String txt, String key) {
		return new String(encrypt(txt.getBytes(), key.getBytes()));
	}

	public static String encrypt(String txt, String key, String encoding)  {
	    String str = null;
	    try {
	      str = new String(encrypt(txt.getBytes(encoding), key.getBytes()));
	    } catch (UnsupportedEncodingException e) {
	      e.printStackTrace();
	    }
	    return str;
	}
  
	public static String encrypt(String txt) {
		return new String(encrypt(txt, KEY, "utf8"));
	}

	public static byte[] decrypt(byte[] txt, byte[] key) {
	    txt = encodeKey(Base64.decode(new String(txt)), key);
	    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
	    for (int i = 0; i < txt.length; ++i) {
	      byte md5 = txt[i];
	      byteOut.write(txt[(++i)] ^ md5);
	    }
	    return byteOut.toByteArray();
	}

	public static String decrypt(String txt, String key)  {
		return new String(decrypt(txt.getBytes(), key.getBytes()));
	}
  
	public static String decrypt(String txt)  {
		return new String(decrypt(txt.getBytes(), KEY.getBytes()));
	}
  
	public static String encryptPwd(String password)  {
	    return MD5Util.getMD5String(Base64.encode(password.getBytes()));
	}

	private static byte[] encodeKey(byte[] txt, byte[] encrypt_key) {
	    encrypt_key = Security.MD5(new String(encrypt_key)).toLowerCase().getBytes();
	    byte ctr = 0;
	    byte[] tmp = new byte[txt.length];
	    for (int i = 0; i < txt.length; ++i) {
	      ctr = (ctr == encrypt_key.length) ? 0 : ctr;
	      byte tmp52_51 = ctr; 
	      ctr = (byte)(tmp52_51 + 1); 
	      tmp[i] = (byte)(txt[i] ^ encrypt_key[tmp52_51]);
	    }
	    return tmp;
	}
  
  
	public static void main(String[] arg) {
//	    String source = System.currentTimeMillis() + "1212";
//	    String key = "asd key";
//	
//	    System.out.println("Source : " + source + " Key : " + KEY);
//	    String encryptTxt = encrypt(source, KEY, "utf8");
//	    System.out.println("Encypt String : " + encryptTxt);
//	    String decryptTxt = decrypt(encryptTxt, KEY);
//	    System.out.println("Decypt String : " + decryptTxt);
//	
//	    byte[] encryptTxt2 = encrypt(source.getBytes(), key.getBytes());
//	    System.out.println("Encypt String : " + new String(encryptTxt2));
//	    byte[] decryptTxt2 = decrypt(encryptTxt2, key.getBytes());
//	    System.out.println("Decypt String : " + new String(decryptTxt2));
	}
	
}

